package priv.example.check;

import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

/**
 * /TODO
 *
 * @author wjh
 **/
public class MethodLimitCheck extends AbstractCheck {
    private static final int DEFAULT_MAX = 50;
    private int max = DEFAULT_MAX;

    public void setMax(int limit) {
        max = limit;
    }

    @Override
    public int[] getDefaultTokens() {
        return new int[]{TokenTypes.CLASS_DEF, TokenTypes.INTERFACE_DEF};
    }

    public int[] getAcceptableTokens() {
        return new int[0];
    }

    public int[] getRequiredTokens() {
        return new int[0];
    }

    @Override
    public void visitToken(DetailAST ast) {
        DetailAST objBlock = ast.findFirstToken(TokenTypes.OBJBLOCK);

        int methodDefs = objBlock.getChildCount(TokenTypes.METHOD_DEF);

        if (methodDefs > this.max) {
            log(1, "max.files.exceeded", Integer.valueOf(this.max));
        }
    }
}
